<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class CallController extends Controller
{
	//添加客服
   public function getAdd()
   {
        return view('admin.call.add');
   }

   //客服列表
   public function getList(Request $request)
   {	
   		$search = $request -> input('search','');
   		$count = $request -> input('count',10);
   		//dd($search);
   		//查询数据 并且分页
   		$data = DB::table('callcenter') ->where('call_name','like','%'.$search.'%') -> paginate($count);
   		return view('admin.call.list',['data' => $data,'request' => $request -> all()]);
   }

   public function postInsert(Request $request)
   {
   		//自动表单验证
   		$this -> validate($request,[
   				'call_name' => 'required',
   				'call_qq' => 'required|numeric',
   				'call_phone' => 'required|regex:/^1[34578][0-9]{9}$/',
   				'call_email' => 'required|email',
   			],[
   				'call_name.required' => '用户名不能为空',
   				'call_qq.required' => 'QQ号不能为空',
   				'call_qq.numeric' => 'QQ号输入有误',
   				'call_phone.required' => '手机号不能为空',
   				'call_phone.regex' => '手机号不正确',
   				'call_email.required' => '邮箱不能为空',
   				'call_email.email' => '邮箱地址不正确'
   			]);
   		//获取数据  组装
   		$status = ( $request -> input('call_status') == 1 ) ? '显示' : '不显示';

   		$arr = $request -> only('call_name','call_qq','call_phone','call_email');

   		$arr['call_status'] = $status; 
   		//插入数据库
   		$res = DB::table('callcenter') -> insert($arr);
   		//判断是否插入成功
   		if($res)
   		{
   			return redirect('admin/call/list') -> with('success','添加成功');
   		}else
   		{
   			return back() -> with('error','添加失败');
   		}
   }
    public function getEdit($id)
  	{
  		//执行修改操作
  		$edit = DB::table('callcenter') -> where('call_id',$id) -> first();
  		return view('admin.call.edit',['data' => $edit]);
  	} 

  	public function postUpdate(Request $request)
  	{	
  		//自动表单验证
   		$this -> validate($request,[
   				'call_name' => 'required',
   				'call_qq' => 'required|numeric',
   				'call_phone' => 'required|regex:/^1[34578][0-9]{9}$/',
   				'call_email' => 'required|email',
   			],[
   				'call_name.required' => '用户名不能为空',
   				'call_qq.required' => 'QQ号不能为空',
   				'call_qq.numeric' => 'QQ号输入有误',
   				'call_phone.required' => '手机号不能为空',
   				'call_phone.regex' => '手机号不正确',
   				'call_email.required' => '邮箱不能为空',
   				'call_email.email' => '邮箱地址不正确'
   			]);
   		//获取数据  组装
   		$status = ( $request -> input('call_status') == 1 ) ? '显示' : '不显示';

   		$arr = $request -> only('call_name','call_qq','call_phone','call_email');

   		$arr['call_status'] = $status; 
   		//插入数据库
   		$arr = $request -> except(['_token','call_id']);
  		$update = DB::table('callcenter') -> where('call_id',$request -> input('call_id')) -> update($arr);
   		//判断是否插入成功
   		if($update)
   		{
   			return redirect('admin/call/list') -> with('success','修改成功');
   		}else
   		{
   			return back() -> with('error','修改失败');
   		}
  		
  	}

  	public function getDelete($id)
  	{
  		//执行删除
  		$delete = DB::table('callcenter') -> where('call_id',$id) -> delete();
  		if($delete){
  			return redirect('admin/call/list') -> with('success','删除成功');
  		}else{
  			return back() -> with('error','删除失败');
  		}
  	}
}
